//
//  UIImage+AsyncDraw.m
//  照片选择器
//
//  Created by 檀志文 on 16/5/20.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "UIImage+AsyncDraw.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@implementation UIImage (AsyncDraw)


-(void)hm_AsyncDrawImagesize:(CGSize)size bkgColor:(UIColor *)bkgColor isCorner:(BOOL)isCorner drawFinish:(void(^)(UIImage *image))drawFinish{
//  别人没去传入数据
    if ((size.width ==0) && (size.height == 0)) {
        size = self.size;
    }
//  上下文大小的rect
    CGRect rect = CGRectMake(CGPointZero.x, CGPointZero.y, size.width, size.height);
//  开启上下文
    UIGraphicsBeginImageContextWithOptions(size, bkgColor!=nil,[UIScreen mainScreen].scale);
//  设置背景色
    [bkgColor setFill];
    UIRectFill(rect);
//  需要圆角
    if (isCorner) {
//      路径
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:rect];
//        让后面绘制的元素在路径里面
        [bezierPath addClip];
    }
    [self drawInRect:rect];
//  结束上下文
    // 获取图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
//  返回绘制好的图片
    dispatch_async(dispatch_get_main_queue(), ^{
        if (drawFinish) {
            drawFinish(newImage);
        }
    });
}

@end
